package com.cucn.ingmall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.cucn.common.valid.AddGroup;
import com.cucn.common.valid.UpdateGroup;
import com.cucn.common.valid.UpdateStatusGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.cucn.ingmall.product.entity.BrandEntity;
import com.cucn.ingmall.product.service.BrandService;
import com.cucn.common.utils.PageUtils;
import com.cucn.common.utils.R;


/**
 * 品牌
 *
 * @author kouchaojie
 * @email kou1688@foxmail.com
 * @date 2021-11-17 17:07:49
 */
@RestController
@RequestMapping("product/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 条件查询
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = brandService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{brandId}")
    public R info(@PathVariable("brandId") Long brandId) {
        BrandEntity brand = brandService.getById(brandId);

        return R.ok().put("brand", brand);
    }

    /**
     * 根据id批量查询品牌数据
     *
     * @param brandIds 品牌idList
     * @return 品牌数据list
     */
    @GetMapping("/infos")
    public R infos(@RequestParam("brandIds") List<Long> brandIds) {

        List<BrandEntity> brandList = brandService.getBrandsByIds(brandIds);

        return R.ok().put("brand", brandList);
    }

    /**
     * 保存品牌数据(需要进行数据校验)
     *
     * @param brand 品牌信息
     * @return 统一结果返回
     */
    @RequestMapping("/save")
    public R save(@RequestBody @Validated({AddGroup.class}) BrandEntity brand) {
        brandService.save(brand);
        return R.ok();
    }

    /**
     * 修改品牌信息
     *
     * @param brand 修改的品牌信息
     * @return 统一结果返回
     */
    @RequestMapping("/update")
    public R update(@RequestBody @Validated({UpdateGroup.class}) BrandEntity brand) {
        brandService.updateDetail(brand);

        return R.ok();
    }

    /**
     * 修改品牌显示状态
     */
    @RequestMapping("/update/status")
    public R updateStatus(@RequestBody @Validated({UpdateStatusGroup.class}) BrandEntity brand) {
        brandService.updateById(brand);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] brandIds) {
        brandService.removeByIds(Arrays.asList(brandIds));

        return R.ok();
    }

}
